{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "---\n",
    "title: Python integration\n",
    "sidebar_position: 1\n",
    "description: Using Python and Mojo together.\n",
    "css: /static/styles/page-navigation.css\n",
    "website:\n",
    "  open-graph:\n",
    "    image: /static/images/mojo-social-card.png\n",
    "  twitter-card:\n",
    "    image: /static/images/mojo-social-card.png\n",
    "---"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Our long-term goal is to make Mojo a *superset of Python* (that is, to make Mojo \n",
    "compatible with existing Python programs). Python programmers should be able to\n",
    "use Mojo immediately, and be able to access the huge ecosystem of Python \n",
    "packages that are available today. \n",
    "\n",
    "However, Mojo is still in early development and many Python features are not yet\n",
    "implemented. You can't currently write everything in Mojo that you can write in\n",
    "Python. And Mojo doesn't have its own ecosystem of packages yet.\n",
    "\n",
    "To help bridge this gap, Mojo lets you import Python modules, call Python \n",
    "functions and interact with Python objects from Mojo code. It runs Python code\n",
    "using a standard Python interpreter (CPython), so your existing Python code\n",
    "doesn't need to change.\n",
    "\n",
    "## Import a Python module\n",
    "\n",
    "To import a Python module in Mojo, just call \n",
    "[`Python.import_module()`](/mojo/stdlib/python/python.html#import_module) \n",
    "with the module name:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from python import Python\n",
    "\n",
    "fn use_array() raises:\n",
    "    # This is equivalent to Python's `import numpy as np`\n",
    "    var np = Python.import_module(\"numpy\")\n",
    "\n",
    "    # Now use numpy as if writing in Python\n",
    "    var array = np.array([1, 2, 3])\n",
    "    print(array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3]\n"
     ]
    }
   ],
   "source": [
    "use_array()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Yes, this imports Python NumPy, and you can import *any other Python module*\n",
    "that you have installed.\n",
    "\n",
    "A few things to note:\n",
    "\n",
    "- Currently, you cannot import individual members (such as a single Python class\n",
    "  or function)—you must import the whole Python module and then access members\n",
    "  through the module name.\n",
    "\n",
    "- Mojo doesn't yet support top-level code, so the `import_module()` call must\n",
    "  be inside another method. This means you may need to import a module multiple\n",
    "  times or pass around a reference to the module. This works the same way as \n",
    "  Python: importing the module multiple times won't run the initialization\n",
    "  logic more than once, so you don't pay any performance penalty.\n",
    "\n",
    "- `import_module()` may raise an exception (for example, if the module isn't\n",
    "  installed). If you're using it inside an `fn` function, you need to either\n",
    "  handle errors (using a `try/except` clause), or add the `raises` keyword to\n",
    "  the function signature. You'll also see this when calling Python functions\n",
    "  that may raise exceptions. (Raising exceptions is much more common in Python\n",
    "  code than in the Mojo standard library, which \n",
    "  [limits their use for performance reasons](/mojo/roadmap.html#the-standard-library-has-limited-exceptions-use).)\n",
    "\n",
    ":::note\n",
    "\n",
    "Mojo loads the Python interpreter and Python modules at runtime, so\n",
    "wherever you run a Mojo program, it must be able to access a compatible Python\n",
    "interpreter, and to locate any imported Python modules. For more information,\n",
    "see [Python environment](#python-environment).\n",
    "\n",
    ":::"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Import a local Python module\n",
    "\n",
    "If you have some local Python code you want to use in Mojo, just add\n",
    "the directory to the Python path and then import the module.\n",
    "\n",
    "For example, suppose you have a Python file named `mypython.py`:\n",
    "\n",
    "```{.python filename=\"mypython.py\"}\n",
    "import numpy as np\n",
    "\n",
    "def gen_random_values(size, base):\n",
    "    # generate a size x size array of random numbers between base and base+1\n",
    "    random_array = np.random.rand(size, size)\n",
    "    return random_array + base\n",
    "```\n",
    "\n",
    "Here's how you can import it and use it in a Mojo file:\n",
    "\n",
    "```{.mojo filename=\"main.mojo\"}\n",
    "from python import Python\n",
    "\n",
    "fn main() raises:\n",
    "    Python.add_to_path(\"path/to/module\")\n",
    "    var mypython = Python.import_module(\"mypython\")\n",
    "\n",
    "    var values = mypython.gen_random_values(2, 3)\n",
    "    print(values)\n",
    "```\n",
    "\n",
    "Both absolute and relative paths work with \n",
    "[`add_to_path()`](/mojo/stdlib/python/python.html#add_to_path). For example, you\n",
    "can import from the local directory like this:\n",
    "\n",
    "```mojo\n",
    "Python.add_to_path(\".\")\n",
    "```\n",
    "\n",
    "## Call Mojo from Python\n",
    "\n",
    "As shown above, you can call out to Python modules from Mojo. However, there's \n",
    "currently no way to do the reverse—import Mojo modules from Python or call Mojo\n",
    "functions from Python.\n",
    "\n",
    "This may present a challenge for using certain modules. For example, many UI \n",
    "frameworks have a main event loop that makes callbacks to user-defined code\n",
    "in response to UI events. This is sometimes called an \"inversion of control\" \n",
    "pattern. Instead of your application code calling *in* to a library, the \n",
    "framework code calls *out* to your application code.\n",
    "\n",
    "This pattern doesn't work because you can't pass Mojo callbacks to a Python \n",
    "module.\n",
    "\n",
    "For example, consider the popular [Tkinter package](https://docs.python.org/3/library/tkinter.html). \n",
    "The typical usage for Tkinter is something like this:\n",
    "\n",
    "- You create a main, or \"root\" window for the application.\n",
    "- You add one or more UI widgets to the window. The widgets can have associated\n",
    "  callback functions (for example, when a button is pushed).\n",
    "- You call the root window's `mainloop()` method, which listens for events, \n",
    "  updates the UI, and invokes callback functions. The main loop keeps running\n",
    "  until the application exits.\n",
    "\n",
    "Since Python can't call back into Mojo, one alternative is to have the Mojo\n",
    "application drive the event loop and poll for updates. The following example\n",
    "uses Tkinter, but the basic approach can be applied to other packages.\n",
    "\n",
    "First we create a Python module that defines a Tkinter interface, with a window\n",
    "and single button:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%python\n",
    "import tkinter as tk\n",
    "\n",
    "class App:\n",
    "    def __init__(self):\n",
    "        self._root = tk.Tk()\n",
    "        self.clicked = False\n",
    "\n",
    "    def click(self):\n",
    "        self.clicked = True\n",
    "\n",
    "    def create_button(self, button_text: str):\n",
    "        button = tk.Button(\n",
    "            master=self._root,\n",
    "            text=button_text,\n",
    "            command=self.click\n",
    "        )\n",
    "        button.place(relx=0.5, rely=0.5, anchor=tk.CENTER)\n",
    "\n",
    "    def create(self, res: str):\n",
    "        self._root.geometry(res)\n",
    "        self.create_button(\"Hello Mojo!\")\n",
    "\n",
    "    def update(self):\n",
    "        self._root.update()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can call this module from Mojo like this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from python import Python\n",
    "\n",
    "fn button_clicked():\n",
    "    print(\"Hi from a Mojo🔥 fn!\")\n",
    "\n",
    "def main():\n",
    "    Python.add_to_path(\".\")\n",
    "    var app = Python.import_module(\"myapp\").App()\n",
    "    app.create(\"800x600\")\n",
    "\n",
    "    while True:\n",
    "        app.update()\n",
    "        if app.clicked:\n",
    "            button_clicked()\n",
    "            app.clicked = False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Instead of the Python module calling the Tkinter `mainloop()` method, the Mojo \n",
    "code calls the `update()` method in a loop and checks the `clicked` attribute \n",
    "after each update.\n",
    "\n",
    "## Python environment\n",
    "\n",
    "The Mojo SDK depends on an existing installed version of Python that includes\n",
    "a shared library version of the Python interpreter. When you install the Mojo\n",
    "SDK, it tries to locate a compatible version of the Python interpreter and set\n",
    "up Python's `sys.path` to load matching modules. In most cases this just works\n",
    "and you don't have to do any further configuration of your Python environment. \n",
    "\n",
    "If you run into problems after installing Mojo, see the following sections.\n",
    "\n",
    "### Installation issues\n",
    "\n",
    "When the installer runs, it tries to locate the CPython shared library using the \n",
    "[find_libpython](https://pypi.org/project/find-libpython/) module.\n",
    "\n",
    "This may fail if one of the following is true:\n",
    "\n",
    "- There is no version of Python installed, or the installed version isn't\n",
    "  supported by the Mojo SDK.\n",
    "\n",
    "- The installer can't find a shared library version of the CPython interpreter \n",
    "  (for example, `.so` or `.dylib` file). Some Python distributions don't include\n",
    "  shared libraries, which prevents Mojo from embedding the interpreter.\n",
    "\n",
    "If one of these things is the case, you'll need to install a compatible version\n",
    "of Python that includes shared libraries. Try following the instructions in \n",
    "[Set up a Python environment with Conda](#set-up-a-python-environment-with-conda)\n",
    "to install a virtual environment. \n",
    "\n",
    "### Set up a Python environment with Conda\n",
    "\n",
    "Using a Python virtual environment like \n",
    "[Conda](https://docs.conda.io/en/latest/) is one way to avoid problems with \n",
    "your Python installation. This provides a consistent Python environment with a\n",
    "known version of Python and all of the Python packages you want to use with\n",
    "Mojo.\n",
    "\n",
    "To set up a virtual environment with Conda:\n",
    "\n",
    "1. Install Conda by following the \n",
    "   [Quick command-line install instructions](https://docs.conda.io/projects/miniconda/en/latest/#quick-command-line-install).\n",
    "\n",
    "   Make sure to initialize Conda for the shell or shells you use, for example:\n",
    "\n",
    "   ```bash\n",
    "   ~/miniconda3/bin/conda init zsh\n",
    "   ```\n",
    "\n",
    "   Or:\n",
    "\n",
    "   ```bash\n",
    "   ~/miniconda3/bin/conda init --all\n",
    "   ```\n",
    "\n",
    "2. Restart your shell. \n",
    "\n",
    "3. Run the following command to configure Mojo to use the Python shared library\n",
    "   from your Conda environment:\n",
    "\n",
    "   ```bash\n",
    "   export MOJO_PYTHON_LIBRARY=\"$(find $CONDA_PREFIX/lib -iname 'libpython*.[s,d]*' | sort -r | head -n 1)\"\n",
    "   echo \"export MOJO_PYTHON_LIBRARY=$MOJO_PYTHON_LIBRARY\" >> ~/.zshrc\n",
    "   ```\n",
    "\n",
    "   **Note:** If you're using a shell other than zsh, you'll need to adjust these\n",
    "   commands. For example, if you're using bash, replace `.zshrc` with the\n",
    "   shell configuration file you use, such as `.bashrc` or `.bash_profile`.\n",
    "\n",
    "4. Try running the Mojo REPL:\n",
    "\n",
    "   ```bash\n",
    "   mojo\n",
    "   ```\n",
    "\n",
    "After setting up the Conda virtual environment, you can install any Python \n",
    "packages you want to use with Mojo using the `conda install` command. For\n",
    "example:\n",
    "\n",
    "```bash\n",
    "conda install numpy\n",
    "```\n",
    "\n",
    "For more information on using Conda with Mojo, see \n",
    "[Using Mojo with Python](https://www.modular.com/blog/using-mojo-with-python) on\n",
    "the Modular Blog."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Mojo",
   "language": "mojo",
   "name": "mojo-jupyter-kernel"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "mojo"
   },
   "file_extension": ".mojo",
   "mimetype": "text/x-mojo",
   "name": "mojo"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
